package com.klun.project.log;

import org.slf4j.Logger;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.LoggerFactory;

public abstract class KLog implements Logger, Constants {
	private static KLog LOGGERBUILDER = initLogger();
	private static final Map<MethodClassKey, String> readableMethodIds = new ConcurrentHashMap(1024);

	public KLog() {
	}

	public static KLog getLogger(Class<?> clazz) {
		return getLogger(clazz.getName());
	}

	public static KLog getLogger(String loggerName) {
		return LOGGERBUILDER.createNewLogger(loggerName);
	}

	protected abstract KLog createNewLogger(String var1);

	public abstract void logTraceInfoTMDC();

	public abstract void cleanTraceInfoInMDC();

	public abstract void extInfo(String var1, EventPair var2);
	// todo whm 先注释掉 作用？
//	public abstract Tracer buildTracer();

	public abstract void launchOnlineChange();

	private static KLog initLogger() {
		String DEFAULT_GMF_LOGGER_BINDER = "com.klun.project.log.KLogImpl";
		try {
			return (KLog) Class.forName(DEFAULT_GMF_LOGGER_BINDER).newInstance();
		} catch (Exception var4) {
			EmptyKLogImpl emptyGmfLoggerImpl = new EmptyKLogImpl();
			Logger logger = LoggerFactory.getLogger(KLog.class);
			logger.error("no advance logger jar");
			return emptyGmfLoggerImpl;
		}
	}

	public static String getReadableMethodId(Class clz, Method method) {
		MethodClassKey methodClassKey = new MethodClassKey(method, clz);
		String methodId = (String) readableMethodIds.get(methodClassKey);
		if (methodId == null) {
			String params = "";
			Class<?>[] parameterTypes = method.getParameterTypes();
			for (int j = 0; j < parameterTypes.length; ++j) {
				if (parameterTypes[j].isArray()) {
					try {
						Class<?> cl = parameterTypes[j];
						int dimensions;
						for (dimensions = 0; cl.isArray(); cl = cl.getComponentType()) {
							++dimensions;
						}
						StringBuilder sb = new StringBuilder();
						sb.append(cl.getName());
						for (int i = 0; i < dimensions; ++i) {
							sb.append("A");
						}
						params = params + sb;
					} catch (Throwable var11) {
					}
				} else {
					params = params + parameterTypes[j].getName();
				}
				if (parameterTypes.length > 1 && j != parameterTypes.length - 1) {
					params = params + ",";
				}
			}
			methodId = clz.getName() + "." + method.getName() + "(" + params + ")";
			readableMethodIds.put(methodClassKey, methodId);
		}
		return methodId;
	}
}



